﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Matriculex.UI
{
    public class AjaxResponseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();
            if (!isAjaxRequest) return;

            var temp = filterContext.Controller.TempData;
            var data = temp["message"] != null ?
                new { response_type = "success", message = temp["message"] } :
                new { response_type = "error", message = temp["error"] };

            filterContext.Result = new JsonResult
            {
                Data = data
            };
        }
    }
}
